package is.web.xdumper.client;

import is.bean.entity.xdumper.session.Action;
import is.bean.entity.xdumper.session.XdumperSession;
import is.bean.entity.xdumper.session.XdumperSessionBeanRemote;
import is.bean.entity.xdumper.session.XdumperSessionBeanRemoteHome;
import java.rmi.RemoteException;
import java.util.Date;
import javax.ejb.CreateException;
import javax.ejb.FinderException;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;

/**
 * Session support
 *
 * @author aspr0
 */
public class XdumperClientSession
{
    private XdumperSessionBeanRemoteHome sessionHome;

    public XdumperClientSession() throws NamingException
    {
        Object xdsRef = new InitialContext().lookup("ejb/XdumperSessionBean");
        sessionHome =
                (XdumperSessionBeanRemoteHome) PortableRemoteObject.narrow(xdsRef, XdumperSessionBeanRemoteHome.class);
    }

    public Integer startSession(Action action) throws CreateException, RemoteException
    {
        XdumperSession session = new XdumperSession();
        session.setAction(action);
        return sessionHome.createInstance(session).getData().getId();
    }

    public void endSession(Integer id, String filepath) throws FinderException, RemoteException
    {
        XdumperSession session = new XdumperSession();
        session.setFilePath(filepath);
        session.setTimeEnd(new Date().getTime());
        XdumperSessionBeanRemote sessionRemote = sessionHome.findByPrimaryKey(id);
        sessionRemote.setData(session);
    }
}
